package com.hualan.sfzy.service.impl.consult;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hualan.sfzy.constant.CommonConstant;
import com.hualan.sfzy.mapper.ConsultReservationMapper;
import com.hualan.sfzy.mapper.consult.ConsultChargesMapper;
import com.hualan.sfzy.mapper.consult.ConsultOrderMapper;
import com.hualan.sfzy.model.entity.ConsultCharges;
import com.hualan.sfzy.model.entity.ConsultOrder;
import com.hualan.sfzy.model.entity.ConsultReservation;
import com.hualan.sfzy.model.vo.ConsultOrderVO;
import com.hualan.sfzy.model.vo.OrderVo;
import com.hualan.sfzy.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
// 咨询师订单服务实现类

public class ConsultOrderServiceImpl implements BaseService<ConsultOrder> {
    @Autowired
    private ConsultOrderMapper mapper;
    @Autowired
    private ConsultChargesMapper chargesMapper;
    @Autowired
    private ConsultReservationMapper reservationMapper;
    @Override
    @Transactional
    public int add(ConsultOrder entity) {
        // 添加订单 需要向 咨询师收费标准表中修改 number=number+1
        mapper.insert(entity);

        if(entity.getId()==null){
            return 0;
        }
        ConsultCharges charges = new ConsultCharges();
        charges.setChargesid(entity.getChargesid());
        charges.setConsultid(entity.getConsultid());
        // 还需要向 预约表中添加一条数据
        int i = chargesMapper.updateNumber(charges);
        if(i>0){
            return reservationMapper.insert(entity.getReservation());
        }else{
            return 0;
        }
    }

    // 订单没有修改操作
    @Override
    public int edit(ConsultOrder entity) {
        return 0;
    }

    // 用户删除订单
    @Override
    public int remove(Integer id) {
        return mapper.delete(id);
    }

    // 用户批量删除订单
    @Override
    public int removeBatch(Integer[] ids) {
        return mapper.deleteBatch(ids);
    }

    @Override
    public List<ConsultOrder> find() {
        return mapper.select( null);
    }

    @Override
    public Map<String, Object> findPage(Integer pageNum, Integer pageSize, ConsultOrder entity) {
        PageHelper.startPage(pageNum, pageSize);
        List<ConsultOrder> list = mapper.select(entity);
        PageInfo<ConsultOrder> info = new PageInfo<>(list);
        Map<String, Object> map = new HashMap<>();
        map.put(CommonConstant.PAGE_LIST, list);
        map.put(CommonConstant.PAGE_TOTAL, info.getPages());
        map.put(CommonConstant.TOTAL, info.getTotal());
        return map;
    }

    @Override
    public ConsultOrder findById(Integer id) {
        return mapper.selectById(id);
    }



    // 额外查询方法
    public Map<String, Object> selectPro(Integer pageNum, Integer pageSize,ConsultOrderVO entity) {
        PageHelper.startPage(pageNum, pageSize);
        List<ConsultOrderVO> list = mapper.selectPro(entity);
        Map<String, Object> map = new HashMap<>();
        PageInfo<ConsultOrderVO> info = new PageInfo<>(list);
        map.put(CommonConstant.PAGE_LIST, list);
        map.put(CommonConstant.PAGE_TOTAL, info.getPages());
        map.put(CommonConstant.TOTAL, info.getTotal());
        return map;
    }
    // 获取所有订单
    public List<ConsultOrderVO> selectAll() {
        return mapper.selectAll();
    }
    // 根据 用户id 查询订单
    public Map<String, Object> selectByUserId(Integer pageNum, Integer pageSize,Integer id) {
        PageHelper.startPage(pageNum, pageSize);
        List<OrderVo> list = mapper.selectByUserId(id);
        Map<String, Object> map = new HashMap<>();
        PageInfo<OrderVo> info = new PageInfo<>(list);
        map.put(CommonConstant.PAGE_LIST, list);
        map.put(CommonConstant.PAGE_TOTAL, info.getPages());
        map.put(CommonConstant.TOTAL, info.getTotal());
        return map;
    }
    public Map<String, Object> selectByUserIdPro(Integer pageNum, Integer pageSize,Integer id) {
        PageHelper.startPage(pageNum, pageSize);
        List<OrderVo> list = mapper.selectByUserIdPro(id);
        Map<String, Object> map = new HashMap<>();
        PageInfo<OrderVo> info = new PageInfo<>(list);
        map.put(CommonConstant.PAGE_LIST, list);
        map.put(CommonConstant.PAGE_TOTAL, info.getPages());
        map.put(CommonConstant.TOTAL, info.getTotal());
        return map;
    }




    // 根据 咨询师id 查询订单
    public Map<String, Object> selectByConsultId(Integer pageNum, Integer pageSize,Integer id) {
        PageHelper.startPage(pageNum, pageSize);
        List<ConsultOrderVO> list = mapper.selectByConsultId(id);
        Map<String, Object> map = new HashMap<>();
        PageInfo<ConsultOrderVO> info = new PageInfo<>(list);
        map.put(CommonConstant.PAGE_LIST, list);
        map.put(CommonConstant.PAGE_TOTAL, info.getPages());
        map.put(CommonConstant.TOTAL, info.getTotal());
        return map;
    }
}
